package com.cmf.myproject.controller.boss.task;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmf.myproject.dal.mybatis.enums.TaskStatusEnum;
import com.cmf.myproject.dal.mybatis.filters.TaskQueryFilter;
import com.cmf.myproject.dal.mybatis.model.Task;
import com.cmf.myproject.dal.mybatis.mymodel.TaskProject;
import com.cmf.myproject.dal.mybatis.mymodel.WeeklyViewRspDto;
import com.cmf.myproject.service.project.ProjectService;
import com.cmf.myproject.service.task.TaskFileService;
import com.cmf.myproject.service.task.TaskService;
import com.cmf.myproject.service.task.dto.TaskAddItemReqDto;
import com.cmf.myproject.service.task.dto.TaskUpdateItemReqDto;
import com.cmf.myproject.service.user.UserService;
import com.cmf.myproject.util.dto.layui.SelectMRspDto;
import com.system.core.controller.ControllerBase;
import com.system.core.results.ResultBase;
import com.system.core.view.layui.table.TableViewRspDto;

@Controller
@RequestMapping("boss/task")
public class TaskController extends ControllerBase {
	@Autowired
	private TaskService taskService;
	@Autowired
	private TaskFileService taskFileService;
	@Autowired
	private ProjectService projectService;
	@Autowired
	private UserService userService;
	
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
	
	@RequiresPermissions("task_topage")
	@RequestMapping(value = "to_page",method = RequestMethod.GET)
	public String toPage() {
		getModelMap().put("statusEnums", TaskStatusEnum.getAllEnum());
		getModelMap().put("projectAllList", projectService.projectAllEnableList());
		getModelMap().put("userEnableList", userService.userEnableList());
		return "/boss/task/task-list";
	}
	
	@RequiresPermissions("to_weekly_page")
	@RequestMapping(value = "to_weekly_page",method = RequestMethod.GET)
	public String toWeeklyPage() {
		getModelMap().put("statusEnums", TaskStatusEnum.getAllEnum());
		getModelMap().put("projectAllList", projectService.projectAllEnableList());
		getModelMap().put("userEnableList", userService.userEnableList());
		return "/boss/task/task-weekly-list";
	}
	
	@RequestMapping(value = "export-weekly-report")
	public void exportWeeklyReport(@RequestHeader(required = true) Map<String, String> headers,
			TaskQueryFilter filter,HttpServletResponse response) throws Exception {
		
		response.setHeader("Content-disposition", "attachment; filename="+new String("技术部周报-".getBytes("UTF-8"), "ISO8859-1")+dateFormat.format(Calendar.getInstance().getTime())+".xlsx");
		taskFileService.weeklyExport(filter, response.getOutputStream());
	}
	
	@RequestMapping(value = "task-status-list")
	@ResponseBody
	public List<SelectMRspDto> taskStatusList(){
		return taskService.taskStatusList();
	}
	
	@RequestMapping(value = "query_list",method = RequestMethod.POST)
	@ResponseBody
	public TableViewRspDto<TaskProject> taskQueryList(TaskQueryFilter filter) {
		TableViewRspDto<TaskProject> rsp = taskService.taskList(filter);
		return rsp;
	}
	
	@RequestMapping(value = "query_weekly_list",method = RequestMethod.POST)
	@ResponseBody
	public TableViewRspDto<WeeklyViewRspDto> weeklyList(TaskQueryFilter filter){
		return taskService.weeklyList(filter);
	}
	
	@RequestMapping(value = "get_item_by_key",method = RequestMethod.POST)
	@ResponseBody
	public Task taskGetByPrimaryKey(Task task){
		return taskService.taskGetByPrimaryKey(task);
	}
	
	@RequiresPermissions("task_add_opt")
	@RequestMapping(value = "add_item",method = RequestMethod.POST)
	@ResponseBody
	public ResultBase taskAddItem(@Validated TaskAddItemReqDto taskAddItemReqDto) {
		return taskService.taskSave(taskAddItemReqDto);
	}
	
	@RequiresPermissions("task_edit_opt")
	@RequestMapping(value = "update_item",method = RequestMethod.POST)
	@ResponseBody
	public ResultBase taskUpdateItem(@Validated TaskUpdateItemReqDto taskUpdateItemReqDto) {
		return taskService.taskUpdateByPrimaryKey(taskUpdateItemReqDto);
	}
	
	@RequiresPermissions("task_delete_opt")
	@RequestMapping(value = "delete_item",method = RequestMethod.POST)
	@ResponseBody
	public ResultBase taskDeleteItem(@RequestBody List<Long> keys) {
		return taskService.taskDeleteByPrimaryKey(keys);
	}
}
